//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)


using System;
using WinPcap;

namespace SoapTrace.Core.Communication
{
	/// <summary>
	/// Filters tcp packet's based on a specified IP address and port.
	/// </summary>
	public class TcpPacketFilter
	{
		/// <summary>
		/// The IP address and port in the form "100.1.1.1:80".  
		/// The packets sent to and received from this address pass the filter.
		/// </summary>
		private string m_FullIPAddress = String.Empty;
		
		/// <summary>
		/// The IP address.
		/// </summary>
		private string m_IPAddress = String.Empty;
		
		/// <summary>
		/// The port.
		/// </summary>
		private int m_Port;
		
		/// <summary>
		/// Creates a new Tcp Packet filter.
		/// </summary>
		/// <param name="address">The IP address that packets should be sent
		/// to or sent from.</param>
		/// <param name="port">The port that the packets should be sent to
		/// or sent from.</param>
		public TcpPacketFilter( string address, int port )
		{
			m_FullIPAddress = String.Concat( address, ":", port.ToString( ) );
			m_IPAddress = address;
			m_Port = port;
		}
		
		/// <summary>
		/// Gets a <see cref="TcpPacket"/> if it matches the filter.
		/// </summary>
		/// <param name="packet">A data packet.</param>
		/// <returns>A <see cref="TcpPacket"/> if it matches the filter;
		/// otherwise <see langword="null"/>.</returns>
		public TcpPacket GetTcpPacket( Packet packet )
		{
			TcpPacket TcpPacket = TcpPacket.GetTcpPacket( packet );
			
			if( TcpPacket != null )
			{
				if( PacketPassesFilter( TcpPacket ) )
				{
					// Passes filter.
				}
				else
				{
					TcpPacket = null;
				}
			}
			
			return TcpPacket;			
		}
	
		/// <summary>
		/// Gets a <see cref="TcpPacket"/> if it matches the filter.
		/// </summary>
		/// <param name="packet">A data packet.</param>
		/// <returns>A <see cref="TcpPacket"/> if it matches the filter;
		/// otherwise <see langword="null"/>.</returns>
		public TcpPacket GetTcpPacket( IPPacket packet )
		{
			TcpPacket TcpPacket = null;
			
			if( packet.Protocol == Protocol.Tcp )
			{
				TcpPacket = ( TcpPacket )( packet.InnerPacket );
				if( PacketPassesFilter( TcpPacket ) )
				{
					// OK.
				}
				else
				{
					TcpPacket = null;
				}
			}
			
			return TcpPacket;
		}
		
		/// <summary>
		/// Gets the port number associated with this filter.
		/// </summary>
		public int Port
		{
			get
			{
				return m_Port;
			}
		}
		
		/// <summary>
		/// Gets the IP address associated with this filter.
		/// </summary>
		public string IPAddress
		{
			get
			{
				return m_IPAddress;
			}
		}
		
		/// <summary>
		/// Gets the IP address and port end point string.
		/// </summary>
		/// <remarks>Returns a string of the form "1.1.1.1:80".</remarks>
		public string RemoteEndPoint
		{
			get
			{
				return String.Concat( m_IPAddress, ":", m_Port );
			}
		}
		
		/// <summary>
		/// Checks whether the tcp packet passes the filter.
		/// </summary>
		/// <param name="packet">A tcp packet.</param>
		/// <returns><see langword="true"/> if the packet passes the filter;
		/// otherwise <see langword="false"/>.</returns>
		private bool PacketPassesFilter( TcpPacket packet )
		{
			bool PassedFilter = false;
			
			if( ( packet.DestinationAddress.ToString( ) == m_FullIPAddress ) ||
			   ( packet.SourceAddress.ToString( ) == m_FullIPAddress ) )
			{
				PassedFilter = true;
			}
			
			return PassedFilter;
		}
	}
}
